#include <string>
#include <unordered_map>
using namespace std;

class Solution {
public:
    int minNumberOfFrogs(string croakOfFrogs) {
        unordered_map<char,int> s;
        for(int i = 0 ; i < croakOfFrogs.size() ; i++){
            if(croakOfFrogs[i] == 'c'){
                if(s['k'])  s['k']--;
                s['c']++;      
            }
            if(croakOfFrogs[i] == 'r'){
                if(s['c'] != 0){
                    s['c']--;
                    s['r']++;
                }else   return -1;
            }
            if(croakOfFrogs[i] == 'o'){
                if(s['r'] !=0 ){
                    s['r']--;
                    s['o']++;
                }else   return -1;
            }
            if(croakOfFrogs[i] == 'a'){
                if(s['o']){
                    s['o']--;
                    s['a']++;
                }else   return -1;
            }
            if(croakOfFrogs[i] == 'k'){
                if(s['a']){
                    s['a']--;
                    s['k']++;
                }else return -1;
            }
        }
        for(auto& x: s){
            if(x.second != 0 && x.first != 'k')   return -1;
        }
        return s['k'];
    }
};

class Solution {
public:
    int minNumberOfFrogs(string croakOfFrogs) {
        //可以用int数组存croak，下标代表字母，存的代表次数
        string s("croak");
        int n = s.size();
        vector<int> v(n);//0-c 1-r 2-o 3-a 4-k
        unordered_map<char,int> m;//存储下标和字母的对应关系，方便找前一字母
        for(int i = 0 ; i < n ; i++)    m[s[i]] = i;

        for(auto& ch : croakOfFrogs){
            if(ch == 'c'){
                if(v[m['k']] !=0 ) v[m['k']]--;
                v[m['c']]++;
            }else{
                if(v[m[ch]-1] !=0){
                    v[m[ch]]++;
                    v[m[ch]-1]--;
                }else return -1;
            }
        }
        for(int i = 0 ; i < n-1 ;i++){
            if(v[i] !=0)    return -1;
        }
        return v[n-1];
    }
};